package yw.nmh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import yw.nmh.dicdata.domain.DicDataInfo;
import yw.nmh.dicdata.dto.DataDicValDTO;
import yw.nmh.dicdata.dto.DataDicValQueryDTO;
import yw.nmh.dicdata.vo.DataDicValVO;
import yw.nmh.mapper.DicDataInfoMapper;
import yw.nmh.result.PageDTO;
import yw.nmh.service.DicDataInfoService;
import yw.nmh.utils.BeanConverter;

import java.util.Date;
import java.util.List;

@Service
public class DicDataInfoServiceImpl extends ServiceImpl<DicDataInfoMapper, DicDataInfo> implements DicDataInfoService {

    @Resource
    private DicDataInfoMapper dicDataInfoMapper;
    @Override
    public DicDataInfo One(Integer valId) {
       return dicDataInfoMapper.selectById(valId);
    }

    @Override
    public Page<DataDicValVO> listDataDicVal(Integer dicId, PageDTO pageDTO) {
        Page<DicDataInfo> page = new Page<>(pageDTO.getPageNumber(), pageDTO.getPageSize());
        LambdaQueryWrapper<DicDataInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DicDataInfo::getDicId,dicId);
        Page<DicDataInfo> dataInfoPage = this.page(page, wrapper);

        Page<DataDicValVO> dataDicValVOPage = new Page<>();
        dataDicValVOPage.setRecords(BeanConverter.convertList(dataInfoPage.getRecords(), DataDicValVO.class));
        dataDicValVOPage.setTotal(dataInfoPage.getTotal());
        dataDicValVOPage.setCurrent(dataInfoPage.getCurrent());
        dataDicValVOPage.setSize(dataInfoPage.getSize());
        return dataDicValVOPage;
    }

    @Override
    public Page<DataDicValVO> listByQuery(DataDicValQueryDTO dataDicValQueryDTO) {
        LambdaQueryWrapper<DicDataInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasText(dataDicValQueryDTO.getValValue()),DicDataInfo::getValValue,dataDicValQueryDTO.getValValue())
                .eq(DicDataInfo::getIsEnable,dataDicValQueryDTO.getIsEnable());
        PageDTO pageDTO = new PageDTO();
        pageDTO.setPageNumber(dataDicValQueryDTO.getPageNumber());
        pageDTO.setPageSize(dataDicValQueryDTO.getPageSize());
        Page<DicDataInfo> dicDataInfoPage = new Page<>(pageDTO.getPageNumber(), pageDTO.getPageSize());

        Page<DicDataInfo> page = this.page(dicDataInfoPage, wrapper);
        Page<DataDicValVO> dataDicValVOPage = new Page<>();
        dataDicValVOPage.setRecords(BeanConverter.convertList(page.getRecords(), DataDicValVO.class));
        dataDicValVOPage.setTotal(page.getTotal());
        dataDicValVOPage.setCurrent(page.getCurrent());
        dataDicValVOPage.setSize(page.getSize());

        return dataDicValVOPage;
    }

    @Override
    public Boolean addDicData(DataDicValDTO dataDicValDTO) {

        List<String> strings = dicDataInfoMapper.listValValueByDicId(dataDicValDTO.getDicId());
        Boolean res = true;
        for (String string : strings) {
            if (string.equals(dataDicValDTO.getName())){
                res = false;
            }
        }
        if (res) {
            DicDataInfo dicDataInfo = new DicDataInfo();
            dicDataInfo.setDicId(dataDicValDTO.getDicId());
            dicDataInfo.setValValue(dataDicValDTO.getName());
            dicDataInfo.setIsEnable(1);
            dicDataInfo.setCreateTime(new Date());
            dicDataInfo.setUpdateTime(new Date());
            dicDataInfoMapper.insert(dicDataInfo);
            return true;
        }
        return false;
    }
}
